import pymysql
import random
import re
import string
import redis
import json
from faker import Faker
from multiprocessing import Process, cpu_count

# 指定手机号码前三位
start_num = [133, 149, 153, 162, 173, 177, 180, 181, 189, 130, 131, 132, 145, 155,
             156, 166, 175, 176, 185, 186, 135, 136, 137, 138, 139, 147, 150,
             151, 152, 157, 158, 159, 172, 178, 182, 183, 184, 199]


# 生成随机3位的中文字符
def generate_random_chinese_name_one():
    fake = Faker('zh_CN')
    name = fake.name()
    return name


# 生成随机8位数，再和start_num中随机一个数组成手机号码
def generate_random_phone_number():
    random_pn = random.choice(start_num)
    random_num = str(random.randint(0, 9999999)).zfill(8)
    phone_num = f"{random_pn}{random_num}"
    return phone_num


def generate_five_digit_random_letters_string():
    # 创建一个包含大小写字母和数字的字符集
    chars = string.ascii_letters + string.digits
    random_string = ''.join(random.choice(chars) for _ in range(5))
    return random_string


def generate_five_digit_random_lowercase_string():
    # 创建一个包含小写字母和数字的字符集
    chars = string.ascii_lowercase + string.digits
    random_string = ''.join(random.choice(chars) for _ in range(5))
    return random_string


# 生成随机5位数
def generate_five_digit_random_number():
    return random.randint(10000, 99999)


# 判断初始值是不是一个11位的手机号码
def is_valid_phone_number(phone_number):
    pattern = re.compile(r'^1[3-9]\d{9}$')
    return pattern.match(phone_number)


# 从指定的数据库和数据表中获取用户ID
def get_user_ids(db, database, table, id_field):
    with db.cursor() as cursor:
        cursor.execute(f'SELECT `{id_field}` FROM `{database}`.`{table}`;')
        print("查询 id 完成！")
        return cursor.fetchall()


'''
接收一个数据库连接对象和一些参数，然后用于遍历给定的用户ID列表，查询当前用户的电话号码并更新其电话号码为一个新的随机电话号码。
在此过程中，该函数会对数据库进行修改操作，因此使用了 db.commit() 来提交这些修改操作。
单线程/单进程
'''
# 更新手机号码
def update_phone_numbers(db, user_ids, database, table, phone_field, id_field):
    # 获取数据库游标
    with db.cursor() as cursor:
        # 遍历用户ID列表
        for user_id in user_ids:
            # 查询当前用户的电话号码
            cursor.execute(
                f"SELECT {phone_field} FROM `{database}`.`{table}` WHERE {id_field}=%s;", (user_id[0],))
            current_phone = cursor.fetchone()[0]
            # 如果当前电话号码不为空，也不为 "undefined"
            if current_phone is not None and current_phone != "" and current_phone != "undefined":
                # 生成一个新的随机电话号码
                phone_num = generate_random_phone_number()
                # 更新数据库中的电话号码字段
                cursor.execute(
                    f"UPDATE `{database}`.`{table}` SET {phone_field}=%s WHERE {id_field}=%s;", (phone_num, user_id[0]))
        # 提交所有的数据库修改操作
        db.commit()


'''
用于更新用户表中的字段值。
传入数据库连接对象、需要更新的用户id列表、数据库名、用户表名、身份证号字段名和字段名作为参数。
遍历用户id列表，查询每个用户的当前字段值，如果当前字段值不为空，则生成新的并更新到数据库中。最后提交事务。
单线程/单进程
'''
# 原字段值后面+1
def update_id_numbers(db, user_ids, database, table, id_num, id_field):
    # 获取数据库游标
    with db.cursor() as cursor:
        # 遍历用户id列表
        for user_id in user_ids:
            # 查询当前用户的需要修改的字段值
            cursor.execute(
                f"SELECT {id_num} FROM `{database}`.`{table}` WHERE {id_field}=%s;", (user_id[0],))
            current_id = cursor.fetchone()[0]
            # 如果当前字段值不为空，则生成新的字段值并更新
            if current_id is not None and current_id != "" and current_id != "undefined":
                new_id_num = f"{current_id}1"
                cursor.execute(
                    f"UPDATE `{database}`.`{table}` SET {id_num}=%s WHERE {id_field}=%s;", (new_id_num, user_id[0]))
        # 提交事务
        db.commit()


# 原字段值最后5位变成随机数（数字）
def update_id_num(db, user_ids, database, table, id_num, id_field):
    # 获取数据库游标
    with db.cursor() as cursor:
        # 遍历用户id列表
        for user_id in user_ids:
            # 查询当前用户的需要修改的字段值
            cursor.execute(
                f"SELECT {id_num} FROM `{database}`.`{table}` WHERE {id_field}=%s;", (user_id[0],))
            current_id_num = cursor.fetchone()[0]
            # 如果当前字段值不为空，则生成新的字段值并更新
            if current_id_num is not None and current_id_num != "" and current_id_num != "undefined":
                # 提取原始的前几位
                id_prefix = current_id_num[:-5]
                # 生成一个随机的5位数
                random_number = generate_five_digit_random_number()
                # 将随机5位数拼接
                new_id = f"{id_prefix}{random_number}"
                # 更新
                cursor.execute(
                    f"UPDATE `{database}`.`{table}` SET {id_num}=%s WHERE {id_field}=%s;", (new_id, user_id[0]))
        # 提交事务
        db.commit()


# 原字段值最后5位变成随机数（小写+数字）
def update_id_token(db, user_ids, database, table, id_num, id_field):
    # 获取数据库游标
    with db.cursor() as cursor:
        # 遍历用户id列表
        for user_id in user_ids:
            # 查询当前用户的需要修改的字段值
            cursor.execute(
                f"SELECT {id_num} FROM `{database}`.`{table}` WHERE {id_field}=%s;", (user_id[0],))
            current_id_num = cursor.fetchone()[0]
            # 如果当前字段值不为空，则生成新的字段值并更新
            if current_id_num is not None and current_id_num != "" and current_id_num != "undefined":
                # 提取原始的前几位
                id_prefix = current_id_num[:-5]
                # 生成一个随机的5位数
                random_number = generate_five_digit_random_lowercase_string()
                # 将随机5位数拼接
                new_id = f"{id_prefix}{random_number}"
                # 更新
                cursor.execute(
                    f"UPDATE `{database}`.`{table}` SET {id_num}=%s WHERE {id_field}=%s;", (new_id, user_id[0]))
        # 提交事务
        db.commit()


'''
update_id 函数用于生成新的字段值，并更新到数据库中。
update_id_token_letters_multiprocess 函数则是用多进程的方式更新。
在 update_id 函数中，首先从数据库中查询到用户的字段值，然后检查该字段值是否为空或者 undefined。
如果不是，就根据原始字段值的前几位和一个随机的5位数生成新的字段值，并将其更新到数据库中。
在 update_id_token_letters_multiprocess 函数中，代码使用多进程方式批量更新字段值。
该函数将用户 ID 列表分割成多个块，为每个块创建一个进程，并将该块作为参数传递给 update_id_chunk 函数。
多进程版本
'''
# 原字段值最后5位变成随机数（大小写+数字）
def update_id(db, user_id, database, table, id_num, id_field):
    # 获取数据库游标
    with db.cursor() as cursor:
        # 查询当前用户的需要修改的字段值
        cursor.execute(
            f"SELECT {id_num} FROM `{database}`.`{table}` WHERE {id_field}=%s;", (user_id,))
        current_id_num = cursor.fetchone()[0]
        # 如果当前字段值不为空，则生成新的字段值并更新
        if current_id_num is not None and current_id_num != "" and current_id_num != "undefined":
            # 提取原始的前几位
            id_prefix = current_id_num[:-5]
            # 生成一个随机的5位数
            random_number = generate_five_digit_random_letters_string()
            # 将随机5位数拼接
            new_id = f"{id_prefix}{random_number}"
            # 更新
            cursor.execute(
                f"UPDATE `{database}`.`{table}` SET {id_num}=%s WHERE {id_field}=%s;", (new_id, user_id,))
        # 提交事务
        db.commit()


# 多进程更新
def update_id_token_letters_multiprocess(db_config, user_ids, database, table, id_num, id_field, num_processes=None):
    # 如果未指定进程数，则默认使用CPU核心数
    if num_processes is None:
        num_processes = cpu_count()

    # 计算用户ID数量
    n = len(user_ids)
    # 每个进程处理的用户ID数量
    chunk_size = n // num_processes
    # 将用户ID分成多个块，每块包含chunk_size个ID，用于每个进程处理
    user_ids_chunks = [user_ids[i:i + chunk_size]
                       for i in range(0, n, chunk_size)]

    # 创建一个空的进程列表
    processes = []
    # 遍历用户ID的分块列表
    for user_ids_chunk in user_ids_chunks:
        # 为每个进程创建一个数据库连接
        process_db = create_db_connection(db_config)
        # 创建进程并传入参数
        process = Process(target=update_id_chunk, args=(
            process_db, user_ids_chunk, database, table, id_num, id_field))
        # 将新创建的进程对象添加到进程列表中
        processes.append(process)
        # 启动进程
        process.start()

    # 等待所有进程结束
    for process in processes:
        process.join()


# 更新进程函数
def update_id_chunk(db, user_ids_chunk, database, table, id_num, id_field):
    # 遍历当前进程负责处理的用户ID列表
    for user_id in user_ids_chunk:
        # 调用update_id函数，更新指定字段值
        update_id(db, user_id, database, table, id_num, id_field)
    # 关闭当前进程使用的数据库连接
    db.close()



'''
将指定的数据库表中某个字段等于设置为空值。
'''
# 清空字段值
def update_id_none(db, user_ids, database, table, id_num, id_field):
    # 获取数据库游标
    with db.cursor() as cursor:
        # 查询当前用户的需要修改的字段值并清空
        for user_id in user_ids:
            cursor.execute(
                f"UPDATE `{database}`.`{table}` SET {id_num}=Null WHERE {id_field}=%s;", (user_id[0]))
        # 提交事务
        db.commit()


'''
将指定的数据库表中某个字段等于设置为指定值。
'''
# 更新字段值为指定值
def update_customize(db, user_ids, database, table, id_field, id_num, json_string):
    with db.cursor() as cursor:
        # 查询当前用户的需要修改的字段值
        for user_id in user_ids:
            cursor.execute(
                f"SELECT {id_num} FROM `{database}`.`{table}` WHERE {id_field}=%s;", (user_id[0],))
            current_id_num = cursor.fetchone()[0]
            # 如果当前字段值不为空则操作设置为指定值
            if current_id_num is not None and current_id_num != "" and current_id_num != "undefined":
                # 更新
                cursor.execute(
                    f"UPDATE `{database}`.`{table}` SET {id_num}='{json_string}' WHERE {id_field}=%s;", (user_id[0]))
        # 提交事务
        db.commit()


'''
根据A字段值的值修改B字段值为对应格式的值。
'''
# 多重判断更新
def update_id_more(db, user_ids, database, table, id_num, id_field, id_type):
    with db.cursor() as cursor:
        # 查询当前用户的需要修改的字段值
        for user_id in user_ids:
            cursor.execute(
                f"SELECT {id_num} FROM `{database}`.`{table}` WHERE {id_field}=%s;", (user_id[0]))
            current_id_num = cursor.fetchone()[0]
            # 如果当前字段值不为空则继续操作
            if current_id_num is not None and current_id_num != "" and current_id_num != "undefined":
                # 根据A字段值做判断
                if id_type == "bank":
                    # 提取原始的前几位
                    id_prefix = current_id_num[:-5]
                    # 生成一个随机数
                    random_number = generate_five_digit_random_number()
                    # 将随机数拼接
                    new_id = f"{id_prefix}{random_number}"
                    # 更新
                    cursor.execute(
                        f"UPDATE `{database}`.`{table}` SET {id_num}=%s WHERE {id_field}=%s and account_type='{id_type}';", (new_id, user_id[0]))
                elif id_type == "alipay":
                    # 生成手机号码
                    phone_num = generate_random_phone_number()
                    # 更新
                    cursor.execute(
                        f"UPDATE `{database}`.`{table}` SET {id_num}=%s WHERE {id_field}=%s and account_type='{id_type}'", (phone_num, user_id[0]))
        # 提交事务
        db.commit()


'''
多进程更新中文名，从redis有序集合中获取
'''
# 连接数据库
def create_db_connection(db_config):
    return pymysql.connect(**db_config)


# 连接redis
def create_redis_connection(redis_config):
    return redis.Redis(**redis_config)


# 从redis中获取中文名
# 随机获取
def get_chinese_names(user_ids, zset_key, redis_client):
    # 初始化一个空列表 chinese_names
    chinese_names = []
    # 对于user_ids列表中的每个元素执行代码
    for _ in user_ids:
        # 生成一个随机整数random_index，范围在0和Redis有序集合中元素数量（zcard）减1之间。
        random_index = random.randint(0, redis_client.zcard(zset_key) - 1)
        # 使用zrange方法获取Redis有序集合中指定索引范围内的元素（在这里只获取一个元素，即random_index），并将结果存储在random_name变量中，使用decode方法将其从字节串转换为UTF-8编码的字符串。
        random_name = redis_client.zrange(zset_key, random_index, random_index)[
            0].decode('utf-8')
        # 将random_name添加到chinese_names列表中
        chinese_names.append(random_name)
    return chinese_names


def update_database(args):
    db, user_id, chinese_name, database, table, id_num, id_field = args
    # 获取数据库游标
    with db.cursor() as cursor:
        # 查询待更新字段的值
        cursor.execute(
            f"SELECT {id_num} FROM `{database}`.`{table}` WHERE {id_field}=%s;", (user_id,))
        # 获取查询结果中待更新字段的值
        result = cursor.fetchone()
        current_value = result[0]

        # 如果待更新字段的值满足条件，则执行更新操作
        if current_value is not None and current_value != "" and current_value != "undefined":
            cursor.execute(
                f"UPDATE `{database}`.`{table}` SET {id_num}=%s WHERE {id_field}=%s;", (chinese_name, user_id))
            # 打印更新语句
            print(f"UPDATE `{database}`.`{table}` SET {id_num}=%s WHERE {id_field}=%s;",
                  (chinese_name, user_id))
            # 提交事务
            db.commit()
        else:
            # 如果待更新字段的值不满足条件，则跳过更新操作，并打印提示信息
            print(
                f"跳过更新 {user_id} 更新，因为 {id_num} 没有值，被过滤了。")


'''
调用get_chinese_names函数从Redis中获取随机的中文名字，并将这些名字存储在chinese_names列表中。
然后，它使用zip函数将user_ids_chunk和chinese_names列表中的元素一一对应，得到一个迭代器，每个迭代器元素为一个(user_id, chinese_name)元组。
最后，它将这些元组传递给update_database函数，以更新数据库。
'''
def worker(user_ids_chunk, database, table, id_num, id_field, zset_key, redis_client, db):
    # 从redis中获取随机的中文名字
    chinese_names = get_chinese_names(user_ids_chunk, zset_key, redis_client)
    # 为每个用户更新数据库
    for user_id, chinese_name in zip(user_ids_chunk, chinese_names):
        update_database(
            (db, user_id, chinese_name, database, table, id_num, id_field))


def update_chinese_chars(db_config, user_ids, database, table, id_num, id_field, zset_key, redis_config, num_processes=None):
    # 如果没有指定进程数，则默认使用计算机的 CPU 核心数
    if num_processes is None:
        num_processes = cpu_count()

    # 计算用户ID数量
    n = len(user_ids)
    # 每个进程处理的用户ID数量
    chunk_size = n // num_processes
    # 将用户 ID 划分为多个块，每个块包含 chunk_size 个用户 ID
    user_ids_chunks = [user_ids[i:i + chunk_size]
                       for i in range(0, n, chunk_size)]

    processes = []
    # 对于每个用户 ID 块，创建一个进程来执行 worker 函数更新数据
    for user_ids_chunk in user_ids_chunks:
        # 为每个进程创建独立的数据库连接和 Redis 连接
        process_db = create_db_connection(db_config)
        process_redis_client = create_redis_connection(redis_config)

        # 创建进程，并传递需要的参数
        process = Process(target=worker, args=(user_ids_chunk, database, table,
                          id_num, id_field, zset_key, process_redis_client, process_db))
        processes.append(process)
        process.start()

    # 等待所有进程执行完毕
    for process in processes:
        process.join()


'''
# 单进程更新中文名
def update_chinese_chars_one(db, user_ids, database, table, id_num, id_field, zset_key, redis_client):
    chinese_names = get_chinese_names(user_ids, zset_key, redis_client)

    for user_id, chinese_name in zip(user_ids, chinese_names):
        update_database(
            (db, user_id[0], chinese_name, database, table, id_num, id_field))
'''


# 更新多个字段值为指定值
def update_customize_more(db, user_ids, database, table, id_field, field_value, field_code, value):
    # 获取数据库游标
    with db.cursor() as cursor:
        # 遍历每个用户 ID
        for user_id in user_ids:
            # 执行 SQL 查询语句，查询用户指定字段的值以及代码
            cursor.execute(
                f"SELECT {field_value}, {field_code} FROM `{database}`.`{table}` WHERE {id_field}=%s;", (user_id[0],))
            # 获取查询结果
            result = cursor.fetchone()
            # 如果查询结果不为空
            if result is not None:
                # 获取当前字段的值和代码
                current_value = result[0]
                current_code = result[1]
                # 如果当前值不为空、不为""、不为"undefined"且代码在指定值集合中
                if current_value is not None and current_value != "" and current_value != "undefined" and current_code in value:
                    # 更新用户指定字段的值为"无"
                    cursor.execute(
                        f"UPDATE `{database}`.`{table}` SET {field_value}='无' WHERE {id_field}=%s;", (user_id[0],))
                    # 打印更新成功信息
                    print(
                        f"更新 {user_id[0]}: {field_value} 为 无")
            # 如果查询结果为空
            else:
                # 打印未找到记录信息
                print(f"没有找到 {user_id[0]}")
        db.commit()


# 主函数
def main():
    db_config = {
        'host': '192.168.1.100',
        'user': 'root',
        'password': 'devops',
        'charset': 'utf8mb4'
    }

    redis_config = {
        'host': '127.0.0.1',
        'port': 6379,
        'db': 0
    }

    database = 'qualification_domain'
    table = 't_qualification_audit_record'
    id_field = 'id'
    phone_field = 'creator'
    id_num = 'reason'
    id_type = 'alipay'
    zset_key = 'chinese_names'
    field_value = 'field_value'
    field_code = 'field_code'
    value = ('communityBusinessLicenseSocialCreditCode')


    json_data={"accountId":"544762d8e4b06f557be0633a","accountType":"doctor","auditId":"1484535890304311302","auditTemplateBoxVos":[{"auditFieldVoList":[{"boxCode":"realNameAuditTitle","dataType":"text","fieldCode":"accountId","fieldCodeGrade":1,"fieldName":"医生ID","fieldPosition":1,"fieldValue":"544762d8e4b06f557be0633a","modify":False,"requireUnique":"different","styleCode":"input"},{"boxCode":"realNameAuditTitle","dataFieldId":"1484535890950234112","dataType":"text","fieldCode":"realName","fieldCodeGrade":1,"fieldName":"真实姓名","fieldPosition":2,"fieldValue":"李晟玮","modify":False,"requireUnique":"different","styleCode":"input"},{"boxCode":"realNameAuditTitle","dataFieldId":"1484535891172532226","dataType":"text","fieldCode":"idCardNo","fieldCodeGrade":1,"fieldName":"身份证号码","fieldPosition":3,"fieldValue":"321002197906301521","modify":False,"requireUnique":"unique","styleCode":"input"},{"boxCode":"realNameAuditTitle","childList":[{"dataFieldId":"1484535892003004419","dataType":"date","fieldCode":"idCardStartDate","fieldCodeGrade":2,"fieldName":"身份证有效期开始","fieldParentCode":"idCardVaildDate","fieldPosition":0,"modify":False,"requireUnique":"different","styleCode":"date"},{"dataFieldId":"1484535892179165192","dataType":"date","fieldCode":"idCardEndDate","fieldCodeGrade":2,"fieldName":"身份证有效期结束","fieldParentCode":"idCardVaildDate","fieldPosition":0,"modify":False,"requireUnique":"different","styleCode":"date"}],"dataType":"text","fieldCode":"idCardVaildDate","fieldCodeGrade":1,"fieldName":"身份证有效期","fieldPosition":4,"modify":False,"requireUnique":"different","styleCode":"dateBetween"},{"boxCode":"realNameAuditTitle","dataFieldId":"1484535891503882256","dataType":"text","dictVoList":[{"code":"male","codeGrade":1,"createTime":1642733532000,"dictName":"男","dictType":"sex","id":"51","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"female","codeGrade":1,"createTime":1642733532000,"dictName":"女","dictType":"sex","id":"52","typeGrade":1,"updateTime":1642733532000,"valid":True}],"fieldCode":"sex","fieldCodeGrade":1,"fieldDictType":"sex","fieldName":"性别","fieldPosition":5,"fieldValue":"女","fieldValueCode":"female","modify":False,"requireUnique":"different","styleCode":"picker"},{"boxCode":"realNameAuditTitle","dataFieldId":"1484535891541630992","dataType":"text","dictVoList":[{"code":"achangzu","codeGrade":1,"createTime":1642733532000,"dictName":"阿昌","dictType":"nation","id":"53","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"ewenkezu","codeGrade":1,"createTime":1642733532000,"dictName":"鄂温克","dictType":"nation","id":"54","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"lisuzu ","codeGrade":1,"createTime":1642733532000,"dictName":"傈僳","dictType":"nation","id":"55","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"shuizu","codeGrade":1,"createTime":1642733532000,"dictName":"水","dictType":"nation","id":"56","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"baizu","codeGrade":1,"createTime":1642733532000,"dictName":"白","dictType":"nation","id":"57","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"gaoshanzu","codeGrade":1,"createTime":1642733532000,"dictName":"高山","dictType":"nation","id":"58","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"luobazu","codeGrade":1,"createTime":1642733532000,"dictName":"珞巴","dictType":"nation","id":"59","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"tajikezu","codeGrade":1,"createTime":1642733532000,"dictName":"塔吉克","dictType":"nation","id":"60","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"baoanzu","codeGrade":1,"createTime":1642733532000,"dictName":"保安","dictType":"nation","id":"61","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"gelaozu","codeGrade":1,"createTime":1642733532000,"dictName":"仡佬","dictType":"nation","id":"62","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"manzu","codeGrade":1,"createTime":1642733532000,"dictName":"满","dictType":"nation","id":"63","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"tataerzu","codeGrade":1,"createTime":1642733532000,"dictName":"塔塔尔","dictType":"nation","id":"64","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"bulangzu","codeGrade":1,"createTime":1642733532000,"dictName":"布朗","dictType":"nation","id":"65","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"hanizu","codeGrade":1,"createTime":1642733532000,"dictName":"哈尼","dictType":"nation","id":"66","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"maonanzu","codeGrade":1,"createTime":1642733532000,"dictName":"毛南","dictType":"nation","id":"67","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"tujiazu","codeGrade":1,"createTime":1642733532000,"dictName":"土家","dictType":"nation","id":"68","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"buyizu","codeGrade":1,"createTime":1642733532000,"dictName":"布依","dictType":"nation","id":"69","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"hasakezu","codeGrade":1,"createTime":1642733532000,"dictName":"哈萨克","dictType":"nation","id":"70","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"menbazu","codeGrade":1,"createTime":1642733532000,"dictName":"门巴","dictType":"nation","id":"71","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"tuzu","codeGrade":1,"createTime":1642733532000,"dictName":"土","dictType":"nation","id":"72","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"chaoxianzu","codeGrade":1,"createTime":1642733532000,"dictName":"朝鲜","dictType":"nation","id":"73","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"hanzu","codeGrade":1,"createTime":1642733532000,"dictName":"汉","dictType":"nation","id":"74","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"mengguzu","codeGrade":1,"createTime":1642733532000,"dictName":"蒙古","dictType":"nation","id":"75","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"wazu","codeGrade":1,"createTime":1642733532000,"dictName":"佤","dictType":"nation","id":"76","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"dawoerzu","codeGrade":1,"createTime":1642733532000,"dictName":"达斡尔","dictType":"nation","id":"77","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"hezhezu","codeGrade":1,"createTime":1642733532000,"dictName":"赫哲","dictType":"nation","id":"78","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"miaozu","codeGrade":1,"createTime":1642733532000,"dictName":"苗","dictType":"nation","id":"79","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"weiwuerzu","codeGrade":1,"createTime":1642733532000,"dictName":"维吾尔","dictType":"nation","id":"80","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"daizu","codeGrade":1,"createTime":1642733532000,"dictName":"傣","dictType":"nation","id":"81","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"huizu","codeGrade":1,"createTime":1642733532000,"dictName":"回","dictType":"nation","id":"82","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"mulaozu","codeGrade":1,"createTime":1642733532000,"dictName":"仫佬","dictType":"nation","id":"83","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"wuzibiekezu","codeGrade":1,"createTime":1642733532000,"dictName":"乌孜别克","dictType":"nation","id":"84","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"deangzu","codeGrade":1,"createTime":1642733532000,"dictName":"德昂","dictType":"nation","id":"85","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"jinuozu","codeGrade":1,"createTime":1642733532000,"dictName":"基诺","dictType":"nation","id":"86","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"naxizu","codeGrade":1,"createTime":1642733532000,"dictName":"纳西","dictType":"nation","id":"87","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"xibozu","codeGrade":1,"createTime":1642733532000,"dictName":"锡伯","dictType":"nation","id":"88","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"dongxiangzu","codeGrade":1,"createTime":1642733532000,"dictName":"东乡","dictType":"nation","id":"89","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"jingzu","codeGrade":1,"createTime":1642733532000,"dictName":"京","dictType":"nation","id":"90","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"nuzu","codeGrade":1,"createTime":1642733532000,"dictName":"怒","dictType":"nation","id":"91","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"yaozu","codeGrade":1,"createTime":1642733532000,"dictName":"瑶","dictType":"nation","id":"92","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"dongzu","codeGrade":1,"createTime":1642733532000,"dictName":"侗","dictType":"nation","id":"93","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"jingpozu","codeGrade":1,"createTime":1642733532000,"dictName":"景颇","dictType":"nation","id":"94","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"pumizu","codeGrade":1,"createTime":1642733532000,"dictName":"普米","dictType":"nation","id":"95","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"yizu","codeGrade":1,"createTime":1642733532000,"dictName":"彝","dictType":"nation","id":"96","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"dulongzu","codeGrade":1,"createTime":1642733532000,"dictName":"独龙","dictType":"nation","id":"97","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"keerkezizu","codeGrade":1,"createTime":1642733532000,"dictName":"柯尔克孜","dictType":"nation","id":"98","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"qiangzu","codeGrade":1,"createTime":1642733532000,"dictName":"羌","dictType":"nation","id":"99","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"yuguzu","codeGrade":1,"createTime":1642733532000,"dictName":"裕固","dictType":"nation","id":"100","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"eluosizu","codeGrade":1,"createTime":1642733532000,"dictName":"俄罗斯","dictType":"nation","id":"101","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"lahuzu","codeGrade":1,"createTime":1642733532000,"dictName":"拉祜","dictType":"nation","id":"102","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"salazu","codeGrade":1,"createTime":1642733532000,"dictName":"撒拉","dictType":"nation","id":"103","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"zangzu","codeGrade":1,"createTime":1642733532000,"dictName":"藏","dictType":"nation","id":"104","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"elunchunzu","codeGrade":1,"createTime":1642733532000,"dictName":"鄂伦春","dictType":"nation","id":"105","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"lizu","codeGrade":1,"createTime":1642733532000,"dictName":"黎","dictType":"nation","id":"106","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"shezu","codeGrade":1,"createTime":1642733532000,"dictName":"畲","dictType":"nation","id":"107","typeGrade":1,"updateTime":1642733532000,"valid":True},{"code":"zhuangzu","codeGrade":1,"createTime":1642733532000,"dictName":"壮","dictType":"nation","id":"108","typeGrade":1,"updateTime":1642733532000,"valid":True}],"fieldCode":"nation","fieldCodeGrade":1,"fieldDictType":"nation","fieldName":"民族","fieldPosition":6,"fieldValue":"汉","modify":False,"requireUnique":"different","styleCode":"picker"},{"boxCode":"realNameAuditTitle","dataFieldId":"1484535891621322761","dataType":"text","fieldCode":"idCardAddress","fieldCodeGrade":1,"fieldName":"身份证地址","fieldPosition":7,"modify":False,"requireUnique":"different","styleCode":"input"},{"boxCode":"realNameAuditTitle","dataFieldId":"1484535891709403136","dataType":"date","fieldCode":"birthDay","fieldCodeGrade":1,"fieldName":"出生日期","fieldPosition":8,"modify":False,"requireUnique":"different","styleCode":"date"},{"boxCode":"realNameAuditTitle","dataFieldId":"1484535891852009473","dataType":"text","fieldCode":"idCardIssueOrganization","fieldCodeGrade":1,"fieldName":"签发机关","fieldPosition":9,"modify":False,"requireUnique":"different","styleCode":"input"},{"boxCode":"realNameAuditTitle","childList":[{"dataFieldId":"1484535891218669568","dataType":"image","fieldCode":"idCardFrontImage","fieldCodeGrade":2,"fieldName":"人像面","fieldParentCode":"uploadIdCardImage3","fieldPosition":0,"modify":False,"requireUnique":"different","styleCode":"picture"},{"dataFieldId":"1484535891424190469","dataType":"image","fieldCode":"idCardReverseImage","fieldCodeGrade":2,"fieldName":"国徽面","fieldParentCode":"uploadIdCardImage3","fieldPosition":0,"modify":False,"requireUnique":"different","styleCode":"picture"},{"dataFieldId":"1484535891466133508","dataType":"image","fieldCode":"idCardHoldImage","fieldCodeGrade":2,"fieldName":"手持身份证","fieldParentCode":"uploadIdCardImage3","fieldPosition":0,"modify":False,"requireUnique":"different","styleCode":"picture"}],"dataType":"image","fieldCode":"uploadIdCardImage3","fieldCodeGrade":1,"fieldName":"身份证材料","fieldPosition":10,"modify":False,"requireUnique":"different","styleCode":"picture"}],"boxCode":"realNameAuditTitle","boxName":"医生实名认证","boxPosition":1}],"createTime":1413964797000,"status":"fail"}
    json_string = json.dumps(json_data)

    db = pymysql.connect(**db_config)
    user_ids = get_user_ids(db, database, table, id_field)
    # Remove duplicates from user_ids
    unique_user_ids = list(set(user_ids))
    # 更新手机号
    #update_phone_numbers(db, user_ids, database, table, phone_field, id_field)
    # 后面+1
    #update_id_numbers(db, user_ids, database, table, id_num, id_field)
    # 后5位随机数字
    #update_id_num(db, user_ids, database, table, id_num, id_field)
    # 后5位随机大写+小写+数字
    #update_id_token_letters(db, unique_user_ids, database, table, id_num, id_field)
    #update_id_token_letters_multiprocess(db_config, unique_user_ids, database, table, id_num, id_field)
    # 后5位随机小写+数字
    #update_id_token(db, user_ids, database, table, id_num, id_field)
    # 清空
    #update_id_none(db, unique_user_ids, database, table, id_num, id_field)
    # 设置为任意值
    update_customize(db, user_ids, database, table, id_field, id_num, json_string)
    #update_customize_more(db, user_ids, database, table,
    #                      id_field, field_value, field_code, value)
    # 设置为任意3个中文字符
    #redis_client = redis.Redis(host='127.0.0.1', port=6379, db=0)
    # update_chinese_chars(db_config, unique_user_ids, database,
    #                     table, id_num, id_field, zset_key, redis_config)
    # update_chinese_chars_one(
    #    db, unique_user_ids, database, table, id_num, id_field, zset_key, redis_client)
    # update_chinese_chars(db, user_ids, database, table,
    #                     id_num, id_field, zset_key, redis_client)
    # 多重更新
    #update_id_more(db, user_ids, database, table, id_num, id_field, id_type)

    db.close()


if __name__ == '__main__':
    main()
